from appium.webdriver.common.appiumby import AppiumBy

from selenium.webdriver.support.wait import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC

from logs.log import get_log


class Base():
    #实例化对象
    logger=get_log('APP自动化测试')
    # 利用显示等待封装了一个新的定位元素的方法find_element_new
    # driver    驱动
    # by        定位元素的方法AppiumBy.ID,AppiumBy.XPATH
    # time_out  默认10秒，用于显示等待的时间
    def find_element_new(self,driver,by,value,time_out=10):
        try:
            element=WebDriverWait(driver,time_out).until(EC.visibility_of_element_located((by,value)))
            self.logger.info(f'定位到元素\t{by}---{value}')
            return element
        except Exception as e:
            self.logger.warning(f'定位不到元素\t{by}--{value}')
            self.logger.warning('\t'+str(e))
            return None

    # 重写click方法
    def click_new(self,driver,by,value):
        try:
            el1=self.find_element_new(driver,by,value)
            el1.click()
            self.logger.info(f'已经点击元素\t{by}---{value}')
        except Exception as e:
            self.logger.warning(f'无法点击元素\t{by}---{value}')
            self.logger.warning('\t' + str(e))

    # 重写send_keys方法
    def send_keys_new(self, driver, by, value,text):
        try:
            el1 = self.find_element_new(driver, by, value)
            el1.clear()
            el1.send_keys(text)
            self.logger.info(f'成功输入内容\t{by}---{value}---{text}')
        except Exception as e:
            self.logger.warning(f'无法输入内容\t{by}---{value}---{text}')
            self.logger.warning('\t' + str(e))

if __name__ == '__main__':
    from config.comm_driver import CommonDriver
    from base.base_object import Authorize_object
    dr=CommonDriver().get_driver()
    Base().click_new(dr,AppiumBy.XPATH,'123456')
    Base().click_new(dr,AppiumBy.XPATH,Authorize_object().privacy_agree_button())
